#!/bin/sh

set -e

_away_error() {
	${1} >/dev/null 2>&1
}

_away_all() {
	${1} >/dev/null
}

# Colors
RED='\033[0;31m'; LightBlue='\033[1;34m'; Green='\033[0;32m'

_check_dependency() {
	program="$1"
	_away_all command -v "$program" || { echo "For Installation to work, install \"$program\" first!" && exit 1; }
}

_check_dependency 'wget'
_check_dependency 'curl'

# INSTALL "AM" SYSTEM-WIDE
_install_am() {
	CACHEDIR="${XDG_CACHE_HOME:-$HOME/.cache}"
	mkdir -p "$CACHEDIR" || true
	rm -f "$CACHEDIR"/INSTALL-AM.sh || true
	wget -q https://raw.githubusercontent.com/ivan-hc/AM/main/INSTALL -O "$CACHEDIR"/INSTALL-AM.sh && chmod a+x "$CACHEDIR"/INSTALL-AM.sh
	sudo "$CACHEDIR"/INSTALL-AM.sh && rm -f "$CACHEDIR"/INSTALL-AM.sh
}

# INSTALL "AM" LOCALLY, AS "APPMAN"
_install_appman() {
	ZSHRC="${ZDOTDIR:-$HOME}/.zshrc"
	BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"
	mkdir -p "$BINDIR"
	if ! echo $PATH | grep "$BINDIR" >/dev/null 2>&1; then 
		echo '--------------------------------------------------------------------------'
		echo " Adding $BINDIR to PATH, you might need to"
		echo " close and reopen the terminal for this to take effect."
		if [ -e ~/.bashrc ] && ! grep 'PATH="$PATH:$BINDIR"' ~/.bashrc >/dev/null 2>&1; then
			printf '\n%s\n' 'BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"' >> ~/.bashrc
			printf '\n%s\n' 'if ! echo $PATH | grep "$BINDIR" >/dev/null 2>&1; then' >> ~/.bashrc
			printf '	PATH="$PATH:$BINDIR"\nfi\n' >> ~/.bashrc
		fi
		if [ -e "$ZPROFILE" ] && ! grep 'PATH="$PATH:$BINDIR"' "$ZSHRC" >/dev/null 2>&1; then
			printf '\n%s\n' 'BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"' >> "$ZSHRC"
			printf '\n%s\n' 'if ! echo $PATH | grep "$BINDIR" >/dev/null 2>&1; then' >> "$ZSHRC"
			printf '	PATH="$PATH:$BINDIR"\nfi\n' >> "$ZSHRC"
		fi
	fi
	wget -q https://raw.githubusercontent.com/ivan-hc/AM/main/APP-MANAGER -O "$BINDIR"/appman && chmod a+x "$BINDIR"/appman
}

# CHOOSE BETWEEN "AM" AND "APPMAN"
printf " Choose how to install \"AM\" and all its managed applications.

 1) As \"${RED}AM\033[0m\", command \"${Green}am\033[0m\", this is a system-wide installation:
   - the command is a symlink /usr/local/bin/am for /opt/am/APP-MANAGER
   - all programs will be installed in /opt, into dedicated directories
   - a \"sudo\" password is required both here and to install/remove apps
   - you are the one with read-write permissions for \"AM\" and all programs
   - other users can only use programs you have installed, nothing else
   - other users can still use \"AppMan mode\" for their rootless configurations

 2) As \"${LightBlue}AppMan\033[0m\", command \"${Green}appman\033[0m\", local installation:
   - the command is the script ~/.local/bin/appman
   - choose wherever you want to install all the apps, in your HOME
   - no \"sudo\" required at all
   - you can replicate your configurations on every system you want
   - more storage space required, if more users use \"AppMan\"

"
read -r -p "Choose between \"AM\" (type 1) and \"AppMan\" (2), or leave blank to exit: " response
case "$response" in
	1)	_install_am || exit 1
		;;
	2)	_install_appman || exit 1
		echo '--------------------------------------------------------------------------'
		printf " ${Green}\"AppMan\" has been successfully installed!\033[0m\n"
		printf " Please, run \"${LightBlue}appman -h\033[0m\" to see the list of the options.\n"
		echo '--------------------------------------------------------------------------'
		;;
	''|*)	echo "Installation aborted, exiting." && exit 1
		;;
esac
